package Man.glass.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import Man.glass.entity.glass;
import Man.glass.entity.manager;
import Man.glass.entity.user;

import com.mysql.jdbc.ResultSet;

public class AppManagerDao {
	
	private DBcon db=new DBcon();
	
	public List<user> queryUser(){
		List<user> listuser =new ArrayList<user>();
		ResultSet rs = (ResultSet) db.query("select * from UserTb");
		try {
			while (rs.next()) {
				user u = new user();
				u.setId(rs.getInt("Id"));
				u.setName(rs.getString("Name"));
				u.setPassword(rs.getString("Password"));
				u.setPhone(rs.getString("Phone"));
				u.setEmail(rs.getString("Email"));
				listuser.add(u);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return listuser;
	}
	
	public List<manager> queryManager(){
		List<manager> listmanager =new ArrayList<manager>();
		ResultSet rs = (ResultSet) db.query("select * from ManagerTb");
		try {
			while (rs.next()) {
				manager m = new manager();
				m.setId(rs.getInt("Id"));
				m.setName(rs.getString("Name"));
				m.setPassword(rs.getString("Password"));
				m.setLevelManager(rs.getString("LevelManager"));
				listmanager.add(m);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return listmanager;
	}
	
	public List<glass> queryGlass(){
		List<glass> listglass =new ArrayList<glass>();
		ResultSet rs = (ResultSet) db.query("select * from GlassTb");
		try {
			while (rs.next()) {
				glass g = new glass();
				g.setId(rs.getInt("Id"));
				g.setName(rs.getString("Name"));
				g.setModel(rs.getString("Model"));
				g.setMaterial(rs.getString("Material"));
				g.setUseCrowd(rs.getString("UseCrowd"));
				g.setFrameType(rs.getString("FrameType"));
				g.setCategory(rs.getString("Category"));
				g.setNormalPrice(rs.getString("NormalPrice"));
				g.setMemberPrice(rs.getString("MemberPrice"));
				g.setCommentGlass(rs.getString("CommentGlass"));
				g.setColor(rs.getString("Color"));
				g.setDescription(rs.getString("Description"));
				g.setImage(rs.getString("image"));
				listglass.add(g);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return listglass;
	}
	
	public boolean addUser(user u){
		boolean flag=false;
		String sql="insert into UserTb(Id,Name,Password,Phone,Email) ";
		sql+=" values(?,?,?,?,?)";
		
		Object[] args={u.getId(),
				u.getName(),
				u.getPassword(),
				u.getPhone(),
				u.getEmail()};
		
		int count=db.preExecute(sql, args);
		
		flag=count>0;
		
		return flag;
	}

	public void setDb(DBcon db) {
		this.db = db;
	}

	public DBcon getDb() {
		return db;
	}

}
